package com.acme.domain.bank;

import com.acme.bankapp.service.bank.BankService;
import com.acme.bankapp.service.bank.ClientFactory;

public class BankApplication {

	private static Bank bank;
	private static BankService bankService;
	private static ClientFactory clientFactory;

	public static void main(String[] args) {
		ClientPegistrationListener[] listeners = new ClientPegistrationListener[2];
		listeners[0] = new Bank.PrintClientListener();
		listeners[1] = new Bank.EmailNotificationlistener();
		bank = new Bank(listeners);
		bankService = new BankService();
		clientFactory = new ClientFactory();
		
		try {
			addClients();
		} catch (ClientExistsException e1) {
			e1.printStackTrace();
		}
		try {
			bankService.modifyBank(bank);
		} catch (OverDraftLimitExceededException e) {
			StringBuffer stb = new StringBuffer();
			stb.append("Client's balance = ");
			stb.append(e.getBalance());
			stb.append(", max sum for withdraw = ");
			stb.append(e.getSum());
			System.out.println(stb.toString());
		} catch (NotEnoughFundsException e) {
			e.printStackTrace();
		}
		bankService.printBalance(bank);
		bankService.printMaximumAmountToWithdraw(bank);
		((Bank.EmailNotificationlistener)(bank.getListeners()[1])).getEmailService().close();
	}
	
	private static void addClients() throws ClientExistsException{
		for(int i = 0; i < 10; i++)
			bankService.addClient(clientFactory.getClient(i), bank, i, 
					((Bank.EmailNotificationlistener)(bank.getListeners()[1])).getEmailService().getSender().isTodo());
	}
	
}
